const { db, query } = require("../../db/db");
const util = require("util");
const moment = require("moment");

async function GetWelfareGoodsList(req, res) {
  const WelfareType = req.body.WelfareType;
  const PageSize = req.body.PageSize;
  const PageNum = req.body.PageNum;

  let searchKey = "";

  if (WelfareType !== "") {
    searchKey += util.format("Where WelfareType=%s", WelfareType);
  }

  const Select_welfare_goods = util.format(
    "SELECT \
    WelfareGoodsId,\
    IFNULL(WelfareImg,'') as WelfareImg,\
     IFNULL(WelfareName,'') as WelfareName,\
     WelfareType,\
     IFNULL(WelfareTabs,'') as WelfareTabs,\
     OriginalPrice,\
     CurrentPrice,\
     BuyPeopleNum,\
     IFNULL(WelfareHeightTypeCode,'') as WelfareHeightTypeCode,\
     IFNULL(CreateTime,'') as CreateTime,\
     IFNULL(CreateUser,'') as CreateUser,\
     IFNULL(UpdateTime,'') as UpdateTime,\
     IFNULL(UpdateUser,'') as UpdateUser,\
     IFNULL(CanBuyTimeStart,'') as CanBuyTimeStart,\
     IFNULL(CanBuyTimeEnd,'') as CanBuyTimeEnd,\
     AllNum,\
     DayResidueNum,\
     DayCanBuyNum\
    FROM Welfare_Goods %s\
    ORDER BY WelfareGoodsId DESC\
    LIMIT %s,%s",
    searchKey,
    (PageNum - 1) * PageSize,
    PageSize
  );

  const Select_welfare_goods_res = await query(Select_welfare_goods);

  //获取总数
  const Select_welfare_goods_count = util.format(
    "SELECT \
    COUNT(WelfareGoodsId) as Count\
    FROM Welfare_Goods %s",
    searchKey
  );
  const Select_welfare_goods_count_res = await query(
    Select_welfare_goods_count
  );

  res.send({
    status: 0,
    WelfareGoodsList: Select_welfare_goods_res,
    TotalCount: Select_welfare_goods_count_res[0].Count,
  });
}

module.exports = { GetWelfareGoodsList };
